<?php

session_start();

if(!isset($_SESSION['id'])){
	header('Location:login.php');
}
	$relusername=$_SESSION['username'];
   
	$id=$_POST['id'];
	$connect=mysql_connect("localhost","root","");
   
	mysql_select_db("shenji",$connect);
	mysql_query("set names utf8");
	//得到要删除升级文件的所有信息(以便删除文件)
	$sql2="select * from shenji where id=$id";
	$result2=mysql_query($sql2);
	$row2=mysql_fetch_array($result2);
	$type=$row2['type'];
	$version=$row2['version']*0.01;
	//echo $version;
	//echo $version;exit;
	$move=$row2['move'];
	if(isset($id)){
		//判断version是否为最新的version
		$sql4="select * from type where name='".$type."'";
		$result4=mysql_query($sql4);
		$row4=mysql_fetch_array($result4);
		$last=$row4['last_version'];
		//echo $last;exit;
		//echo $last;exit;
		if($move==$last){
		 //删除后台的升级文件
			$filename ="/var/www/html/upload/upload/".$type."/".$version."_update.tar.gz";
			
			if( is_file( $filename ) ){
				if( unlink($filename) ){
				//然后上传新的版本
				}
			}
			$sql="delete from shenji where id=$id";
			mysql_query($sql);
		
			//更新type中的last_version
			$gversion=$move-1;
			$sql3="update type set last_version=$gversion where name='".$type."'";
			mysql_query($sql3);
	     
		}else{
			$error="只能删除最新的版本";
			echo json_encode($error);
		}
	  
		 //echo "<script>location='select_shenji.php'</script>";
  }
?>